<?php include_once('../includes/header.php');?>
<?php include_once(inc('sql.php'));?>
<?php include_once(inc('repository.php'));?>
<?php
	$group=NULL;
	$group_id=0;
	if(isset($_REQUEST['group_id'])){
		$group = Group::get_by_id(intval($_REQUEST['group_id']));
		if($group)$group_id=$group['id'];
	}
	
?>
<div id="main">
	<span class="panel_title">
		<?php if($group):?>
			<img src="<?php group_icon_url($group_id)?>" width="32" height="32" align="absmiddle"/>
			<a href="<?php group_topics_url($group_id)?>"><?php e($group['name']) ?></a>
			<a href="<?php topic_compose_url($group_id)?>" class="btn btn_new_post">发新贴</a>
		<?php else:?>
			<span>所有主题</span>
			<a href="#" class="btn btn_new_post">发新贴</a>
		<?php endif ?>
	</span>
	<div id="topics" class="content">
		<?php
			$page = isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
			$topics = Topic::get_by_group_id($group_id,$page);
			if(is_array($topics)):
		?>
		<?php foreach ($topics as $topic):?>
			<div class="topic">
				<div class="user">
					<a href="<?php user_url($topic['user_id']);?>" class="img_link">
						<img src="<?php avatar_url($topic['user_id']) ?>" width="48" width="48"/>
					</a>
				</div>
				<div class="summary">
					<div class="title">
						<?php if($topic['is_top']):?>
							<span class="mark_top">置顶</span>
						<?php endif?>
						<a href="<?php topic_url($topic['id'])?>"><?php e($topic['title']);?></a>
					</div>
					<div class="state">
						<?php if(!$group_id): ?>
						<a href="<?php group_topics_url($topic['group_id'])?>"><?php e($topic['group_name']) ?></a>
						::
						<?php endif ?>
						<a href="<?php user_url($topic['user_id']);?>" class="user_name"><?php e($topic['created_by']) ?></a>
						<span class="time">发布于<?php e(time_ago(strtotime($topic['created_at']))); ?></span>
						<?php if($topic['reply_user_id']):?>
							/
							<a href="<?php user_url($topic['reply_user_id']);?>" class="user_name"><?php e($topic['replied_by']) ?></a>
							<span class="time">最后回于复<?php e(time_ago(strtotime($topic['replied_at']))); ?></span>
						<?php endif?>
					</div>
				</div>
				<div class="reply_count"><?php e($topic['count']) ?></div>
				<div class="clear"></div>
			</div>
		<?php endforeach?>
		<?php endif?>
	</div>
	<?php
		$total=Topic::getCount($group_id);
		pagination($total);
	?>
</div>
<?php include_once('group_sidebar.php');?>
<div class="dialog corner hidden" id="group_selector_dialog">
	<div class="dialog_header">请选择小组</div>
	<div class="dialog_content">
		<table border="0">
			<tr>
				<?php
					$groups = Group::get_groups();
					foreach ($groups as $g):
				?>
				<td align="middle" target-url="<?php topic_compose_url($g['id']) ?>">
					<div><img src="<?php group_icon_url($g['id']) ?>" align="middle"></img></div>
					<?php e($g['name']) ?>
				</td>
				<?php endforeach ?>
			</tr>
		</table>
	</div>
	<div class="dialog_footer">
		<a href="javascript:void(0)" class="btn_close_dialog">取消</a>
	</div>
</div>
<?php get_footer();?>